﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Web.UI.HtmlControls;
using System.Web.UI;

using DevExpress.Web.ASPxHeadline;

using LMD.Lib.DataClass;
using LMD.Constant;
using LMD.Lib.Interface;
using LMD.Lib.Oracle;
using LMD.Lib.Controls;

/// <summary>
/// Summary description for CVManager
/// </summary>
public static class CVManager
{
    public static String BuildEditor_CVData(this CVData cvData)
    {    
        String sxOutput = String.Empty;

        Cached.VisitPurposeTable purposeTable = new Cached.VisitPurposeTable();
        DataTable dtPurpose = purposeTable.GetPurpose();

        String sxOTFComboCustomer = CustomControls.Create_OTFComboBox( 
            KCV.CUSTOMER_ID, KLOV.CourtesyCall.COMPANYLIST, null, null, TextboxType.Empty, String.Empty, 
            "SelectCompany()");
        String sxOTFComboContact = CustomControls.Create_OTFComboBox(
            KCV.CP_ID, KLOV.Generic.CONTACTPERSON, null, null, TextboxType.Empty, "-1", String.Empty);
        String sxDateControl = CustomControls.Create_TextBox(KCV.VISIT_DATETIME, KControl.INPUT_ID,
            null, true, TextboxType.DateTime);
        Dictionary<String, String> dicOption = new Dictionary<String, String>();
        dicOption.Add("PLANNED","PLANNED");
        dicOption.Add("DIRECT", "UNPLANNED");
        String sxComboType = CustomControls.Create_ComboBox(KCV.VISIT_STATUS, KControl.INPUT_ID, dicOption, "PLANNED");
        String sxComboPurpose = CustomControls.Create_ComboBox(dtPurpose, KCV.PURPOSE,
            KControl.INPUT_ID, null, "P_VISIT_PURPOSE_ID", "CODE");

        sxOutput += CustomControls.BuildFieldSet_PairedLabel("Company Name", sxOTFComboCustomer);
        sxOutput += CustomControls.BuildFieldSet_PairedLabel("Contact Person", sxOTFComboContact);
        sxOutput += CustomControls.BuildFieldSet_PairedLabel("Visit DateTime", sxDateControl);
        sxOutput += CustomControls.BuildFieldSet_PairedLabel("Visit Status", sxComboType);
        sxOutput += CustomControls.BuildFieldSet_PairedLabel("Visit Purpose", sxComboPurpose);
        
        return sxOutput;
    }

    public static String BuildEditor_EditPlannedDate(String sxControlId, int iCVID, DateTime dtmVisitDatetime)
    {
        String sxOutput = String.Empty;

        String sxDateControl = CustomControls.Create_TextBox(KCV.VISIT_DATETIME, KControl.INPUT_ID,
            dtmVisitDatetime.ToString(), true, TextboxType.DateTime);

        String sxDef = CustomControls.BuildFieldSet_PairedLabel("Visit Date Time", sxDateControl);

        sxOutput = String.Format(
            "<div id=\"{1}_{0}\" class=\"popLOV\">" +
                "<fieldset class=\"m_fs_border\" id=\fs_lov_{0}\">" +
                    "<legend>" +
                        "<a class=\"lnk\" href=\"javascript:toggle('lov_{0}');\">Change Planned Date</a>" +
                    "</legend>" +
                    "<dl class=\"m_fs\"><div id=\"div_invite_{0}\">{2}</div></dl>" +
                    "<div id=\"load\" style=\"float: right; padding-right: 10px;\"></div>" +
                    "<input type=\"button\" id=\"btnCancel_lov_{0}\" class=\"btn\" style=\"float: right; margin-left: 5px; margin-right: 15px;\"" +
                        " onclick=\"closeEditPlannedDate();\" value=\"cancel\" />" +
                    "<input type=\"button\" id=\"btnSave_lov_{0}\" class=\"btn\" style=\"float: right; margin-left: 5px; margin-right: 5px;\"" +
                        " onclick=\"saveEditPlannedDate('{0}');\" value=\"save\" />" +
                    "<div id=\"load_{0}\" style=\"float: right; padding-right: 10px;\"></div>" +
                "</fieldset>" +
            "</div>",
            sxControlId,
            KControl.GENERICPOPFORM_ID,
            sxDef
            );
        return sxOutput;
    }


    public static String BuildFieldSet_CVData(this CVData cvData)
    {
        String sxOutput = String.Empty;

        String cpInfo = String.Empty;
        int icpID = -1;
        String isNewCP = String.Empty;;
        if (cvData.iContactPersonID > 0)
        {
            icpID = cvData.iContactPersonID;
            isNewCP = "N";
        }
        else
        {
            icpID = cvData.iNewContactPersonID;
            isNewCP = "Y";
        }

        cpInfo = String.Format("&nbsp;<a class=\"lnk\" href=\"javascript:ShowPopUp('{0}','{1}');\">(Full Info)</a>",
            icpID.ToString(), isNewCP);

        sxOutput += CustomControls.BuildFieldSet_PairedLabel("Company Name", cvData.sxCompanyName);
        sxOutput += CustomControls.BuildFieldSet_PairedLabel("Contact Person", cvData.sxContactPersonName+cpInfo);
        sxOutput += CustomControls.BuildFieldSet_PairedLabel("Visit Date/Time", cvData.dtmVisitDatetime.ToString("dd-MMM-yyyy HH:mm:ss"));
        sxOutput += CustomControls.BuildFieldSet_PairedLabel("Visit Status", cvData.sxVisitStatus);
        sxOutput += CustomControls.BuildFieldSet_PairedLabel("Create By", cvData.sxCreateBy);

        if (!String.IsNullOrEmpty(cvData.sxResult))
        {
            sxOutput += CustomControls.BuildFieldSet_PairedLabel("Result", cvData.sxResult);
        }
        if (!String.IsNullOrEmpty(cvData.sxFollowUp))
        {
            sxOutput += CustomControls.BuildFieldSet_PairedLabel("Follow Up", cvData.sxFollowUp);
        }
        if (!String.IsNullOrEmpty(cvData.sxVisitPurpose))
        {
            sxOutput += CustomControls.BuildFieldSet_PairedLabel("Visit Purpose", cvData.sxVisitPurpose);
        }
        if (!String.IsNullOrEmpty(cvData.sxCustomerExpectation))
        {
            sxOutput += CustomControls.BuildFieldSet_PairedLabel("Expectation", cvData.sxCustomerExpectation);
        }
        if (cvData.iFile_Upload_ID > 0)
        {
            String sxLinkAttachment = CustomControls.Create_LinkDiv(KCV.FILEUPLOAD,
            "downloadFile('" + cvData.iFile_Upload_ID.ToString() + "');"
            , cvData.sxOriginalFilename);
            sxOutput += CustomControls.BuildFieldSet_PairedLabel("File Uploaded", sxLinkAttachment);
        }
        return sxOutput;
    }

    public static String BuildEditor_UpdateCVData(this CVData cvData)
    {
        String sxOutput = String.Empty;
        Cached.CustomerExpectationTable expectationTable = new Cached.CustomerExpectationTable();
        DataTable dtExpectation = expectationTable.GetExpectation();
        String sxUploadControl = CustomControls.BuildEditor_FileUpload(KCV.FILEUPLOAD,
            KFileHandler.CV.CUSTOMER_VISIT, null, KControl.INPUT_ID, TextboxType.FileUpload);
        String sxComboExpectation = CustomControls.Create_ComboBox(dtExpectation, KCV.EXPECTATION,
            KControl.INPUT_ID, null, "P_CUSTOMER_EXPECTATION_ID", "CODE");
        
        sxOutput += CustomControls.BuildEditor_LabelAndText(KCV.RESULT, KControl.INPUT_ID,
            "Result", null);
        sxOutput += CustomControls.BuildEditor_LabelAndText(KCV.FOLLOWUP, KControl.INPUT_ID,
                    "Follow Up", null);
        sxOutput += CustomControls.BuildFieldSet_PairedLabel("Customer Expectation", sxComboExpectation);
        sxOutput += CustomControls.BuildFieldSet_PairedLabel("Upload File", sxUploadControl);

        return sxOutput;
    }



    public static String BuildReminderLink(DataTable dtReminder, bool bSeeAll)
    {
        String sxOutput = String.Empty;
        
        foreach (DataRow dr in dtReminder.Rows)
        {
           
            sxOutput += String.Format(
            "<table cellspacing=\"0\" cellpadding=\"0\" id=\"{0}\" border=\"0\" class=\"rmT\">" +
                "<tr>" +
                    "<td valign=\"top\"" +
                        "<a class=\"rmH\" href=\"CVReal.aspx?ccid={0}\">{1}</a>" +
                    "</td>" +
                "</tr>" +
                "<tr>" +
                    "<td valign=\"top\">" +
                        "<div class=\"rmCP\">Contact Person : {2}</div>" +
                        "<br/>" +
                        "<div class=\"rmD\">Visit Date Time: {3}</div>" +
                    "</td>" +
                "</tr>" +
            "</table>",
            dr["T_CUSTOMER_VISIT_ID"].ToString(),
            dr["COMPANY_NAME"].ToString(),
            dr["NAME"].ToString(),
            Convert.ToDateTime(dr["VISIT_DATETIME"]).ToString("dd-MMM-yyyy HH:mm:ss")
            );
        }
        if (!bSeeAll)
        {
            sxOutput += "<a class=\"lnk\" href=\"CVReminder.aspx\">See All Reminder</a>";
        }
        
        return sxOutput;
    }

    public static String BuildEditor_CPInfoPopUp(String sxControlId, ContactInfoData cpInfoData)
    {
        String sxOutput = String.Empty;

        String sxDef = String.Empty;
            
        sxDef += CustomControls.BuildFieldSet_PairedLabel("Contact Person", cpInfoData.sxContactName);
        sxDef += CustomControls.BuildFieldSet_PairedLabel("Job Position", cpInfoData.sxJobPositionName);
        sxDef += CustomControls.BuildFieldSet_PairedLabel("Phone Number", cpInfoData.sxPhoneNumber);
        sxDef += CustomControls.BuildFieldSet_PairedLabel("Cell Phone Number", cpInfoData.sxCellPhoneNumber);
        sxDef += CustomControls.BuildFieldSet_PairedLabel("Email Address", cpInfoData.sxEmailAddress);


        sxOutput = String.Format(
            "<div id=\"{1}_{0}\" class=\"popLOV\">" +
                "<fieldset class=\"m_fs_border\" id=\fs_lov_{0}\">" +
                    "<legend>" +
                        "<a class=\"lnk\" href=\"javascript:toggle('lov_{0}');\">Contact Person Info</a>" +
                    "</legend>" +
                    "<dl class=\"m_fs\"><div id=\"div_invite_{0}\">{2}</div></dl>" +
                    "<div id=\"load\" style=\"float: right; padding-right: 10px;\"></div>" +
                    "<input type=\"button\" id=\"btnCancel_lov_{0}\" class=\"btn\" style=\"float: right; margin-left: 5px; margin-right: 15px;\"" +
                        " onclick=\"closeCPInfo();\" value=\"cancel\" />" +
                "</fieldset>" +
            "</div>",
            sxControlId,
            KControl.GENERICPOPFORM_ID,
            sxDef
            );
        return sxOutput;
    }
    public static String BuildEditor_CVSummary()
    {
        String sxOutput = String.Empty;

        String sxOTFCAR = CustomControls.Create_OTFComboBox(KCV.CARID,
            KLOV.Generic.ICPCAR, "", "", TextboxType.Empty, String.Empty, String.Empty);
        String sxStartDate = CustomControls.Create_TextBox(KCV.START,
            KControl.INPUT_ID, String.Empty, true, TextboxType.Date);
        String sxEndDate = CustomControls.Create_TextBox(KCV.END,
            KControl.INPUT_ID, String.Empty, true, TextboxType.Date);
        sxOutput += CustomControls.BuildFieldSet_PairedLabel("CAR", sxOTFCAR);
        sxOutput += CustomControls.BuildFieldSet_PairedLabel("Start Date", sxStartDate);
        sxOutput += CustomControls.BuildFieldSet_PairedLabel("End Date", sxEndDate);
        sxOutput +=
            "<dt>&nbsp;</dt><dd><input type=\"button\" id=\"btnFilter\" class=\"btn\"" +
            "style=\"margin-left: 5px; margin-right: 5px; width:100px;\"" +
                "onclick=\"Filter();\" value=\"FILTER\" />" +
            "<input type=\"button\" id=\"btnAll\" class=\"btn\"" +
            "style=\"margin-left: 5px; margin-right: 5px; width:100px;\"" +
                "onclick=\"ShowAll();\" value=\"SHOW ALL\" /></dd> " +
            "<div id=\"load\" style=\"float: right; margin-left: 5px; margin-right: 5px;\"></div>";
        return sxOutput;
    }

    //add by rista
    public static String BuildEditor_CVList()
    {
        String sxOutput = String.Empty;

        String sxStartDate = CustomControls.Create_TextBox(KCV.START,
            KControl.INPUT_ID, String.Empty, true, TextboxType.Date);
        String sxEndDate = CustomControls.Create_TextBox(KCV.END,
            KControl.INPUT_ID, String.Empty, true, TextboxType.Date);
        sxOutput += CustomControls.BuildFieldSet_PairedLabel("Start Date", sxStartDate);
        sxOutput += CustomControls.BuildFieldSet_PairedLabel("End Date", sxEndDate);
        sxOutput +=
            "<dt>&nbsp;</dt><dd><input type=\"button\" id=\"btnFilter1\" class=\"btn\"" +
            "style=\"margin-left: 5px; margin-right: 5px; width:100px;\"" +
                "onclick=\"Filter1();\" value=\"FILTER\" />" +
            "<input type=\"button\" id=\"btnAll1\" class=\"btn\"" +
            "style=\"margin-left: 5px; margin-right: 5px; width:100px;\"" +
                "onclick=\"ShowAll1();\" value=\"SHOW ALL\" /></dd> " +
            "<div id=\"load\" style=\"float: right; margin-left: 5px; margin-right: 5px;\"></div>";
        return sxOutput;
    }

    public static String Build_HTMLMailBody(this CVData cvData)
    {
        String sxOutput = String.Empty;

        sxOutput = String.Format(
            @"
            Dengan Hormat, <br/>
            Berikut ini adalah data Customer Visit yang akan dilakukan oleh {4},<br/>
            <br/>
            Customer Name       : {0}<br/>
            Contact Person      : {1}<br/>
            Visit Time          : {2}<br/>
            Visit Purpose       : {3}<br/>
            <br/>
            Terima Kasih
            ",
            cvData.sxCompanyName,
            cvData.sxContactPersonName,
            cvData.dtmVisitDatetime.ToString("dd/MMMM/yyyy HH:mm"),
            cvData.sxVisitPurpose,
            cvData.sxCreateBy);

        return sxOutput;
    }
    public static String Build_PlainMailBody(this CVData cvData)
    {
        String sxOutput = String.Empty;

        sxOutput = String.Format(
            @"
            Dengan Hormat,\n
            Berikut ini adalah data Customer Visit yang akan dilakukan oleh {4},\n
            \n
            Customer Name       : {0}\n
            Contact Person      : {1}\n
            Visit Time          : {2}\n
            Visit Purpose       : {3}\n
            \n
            Terima Kasih
            ",
            cvData.sxCompanyName,
            cvData.sxContactPersonName,
            cvData.dtmVisitDatetime.ToString("dd/MMMM/yyyy HH:mm"),
            cvData.sxVisitPurpose,
            cvData.sxCreateBy);

        return sxOutput;
    }
}
